package com.ruicar.afs.cloud.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.admin.dto.holiday.Day;
import com.ruicar.afs.cloud.admin.dto.holiday.Month;
import com.ruicar.afs.cloud.admin.dto.holiday.Week;
import com.ruicar.afs.cloud.admin.entity.SysHolidayInfo;
import com.ruicar.afs.cloud.admin.mapper.SysHolidayInfoMapper;
import com.ruicar.afs.cloud.admin.service.SysHolidayInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
@Slf4j
public class SysHolidayInfoImpl extends ServiceImpl<SysHolidayInfoMapper,SysHolidayInfo> implements SysHolidayInfoService {
    @Override
    public List<Month> getYearHoliday(String year) {
        SysHolidayInfo sysHolidayInfo = this.getOne(Wrappers.<SysHolidayInfo>lambdaQuery().eq(SysHolidayInfo::getYear,year).orderByDesc(SysHolidayInfo::getId),false);
        List<Month> rtnList =new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        StringBuffer stringBuffer = new StringBuffer();
        if(sysHolidayInfo==null){
            calendar.set(Calendar.DAY_OF_YEAR,1);
            calendar.set(Calendar.YEAR,Integer.parseInt(year));
            calendar.set(Calendar.HOUR,1);
            calendar.set(Calendar.MINUTE,1);
            calendar.set(Calendar.SECOND,1);
            while (calendar.get(Calendar.YEAR)==Integer.parseInt(year)){
                if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY) {
                    stringBuffer.append("1");
                }else{
                    stringBuffer.append("0");
                }
                calendar.add(Calendar.DAY_OF_YEAR,1);
            }
        }else{
            stringBuffer.append(sysHolidayInfo.getHolidayInfo());
        }
        calendar.set(Calendar.DAY_OF_YEAR,1);
        calendar.set(Calendar.YEAR,Integer.parseInt(year));
        Month month = new Month();
        month.setMonth(calendar.get(Calendar.MONTH));
        while (calendar.get(Calendar.YEAR)==Integer.parseInt(year)){
            if(calendar.get(Calendar.MONTH)!=month.getMonth()){
                fixMonth(month);
                rtnList.add(month);
                month = new Month();
                month.setMonth(calendar.get(Calendar.MONTH));
            }
            if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY&&month.getLastWeek().getDays().size()>0){
                month.getWeeks().add(new Week());
            }
            if(calendar.get(Calendar.DAY_OF_MONTH)==1){
                for(int i=1;i<calendar.get(Calendar.DAY_OF_WEEK);i++){
                    month.getLastWeek().getDays().add(new Day("-",-1,-1));
                }
            }
            month.getLastWeek().getDays().add(new Day(stringBuffer.charAt(calendar.get(Calendar.DAY_OF_YEAR)-1)+"",calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.DAY_OF_YEAR)-1));
            calendar.add(Calendar.DAY_OF_YEAR,1);
        }
        fixMonth(month);
        rtnList.add(month);
        return rtnList;
    }

    private void fixMonth(Month month){
        int addSize = 7-month.getLastWeek().getDays().size();
        for(int i=0;i<addSize;i++){
            month.getLastWeek().getDays().add(new Day("-",-1,-1));
        }
        while(month.getWeeks().size()<6){
            month.getWeeks().add(new Week());
            addSize = 7-month.getLastWeek().getDays().size();
            for(int i=0;i<addSize;i++){
                month.getLastWeek().getDays().add(new Day("-",-1,-1));
            }
        }
    }
}
